package com.example.om_export.system.config.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * shiro 授权类
 * @author 80511
 */
public class ShiroRealm extends AuthorizingRealm {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

//    @Autowired
//    private OrgUserService orgUserService;
//
//    @Autowired
//    private OrgPermissionService orgPermissionService;
//
//    @Autowired
//    private OrgRoleService orgRoleService;

    /**
     * 提供权限信息
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        log.debug("shiro-获取权限，调用数据库查询");
//        OrgUser orgUser = (OrgUser) principalCollection.getPrimaryPrincipal();
//        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        // 设置角色
//        simpleAuthorizationInfo.setRoles(orgRoleService.getRoleDescByUser(orgUser.getId()));
//        // 设置权限
//        simpleAuthorizationInfo.setStringPermissions(orgPermissionService.getPermissionByUser(orgUser.getId()));
//
//        return simpleAuthorizationInfo;
        return  null;
    }

    /**
     * 提供账户信息
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        log.debug("shiro-获取用户，调用数据库查询");
//        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
//        String username = token.getUsername();
//        // 用用户名查询数据,可以查询用户名，手机号，邮件
//        OrgUser orgUser = orgUserService.getUserForLogin(username);
//        if(orgUser==null){
//            throw new UnknownAccountException("帐号不存在");
//        }
//
//        // 如果没有传过来密码表示不需要密码就可以登录
//        if(token.getPassword()==null){
//            token.setPassword(orgUser.getUserPwd().toCharArray());
//        }else{
//            String tmp = BaseUtil.md5NoSalt(new String(token.getPassword()),3);
//            token.setPassword(tmp.toCharArray());
//        }
//        // 有点淡
////        ByteSource byteSource = ByteSource.Util.bytes(orgUser.getUserid());
//        return new SimpleAuthenticationInfo(orgUser,orgUser.getUserPwd(),getName());
        return null;
    }
}
